package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.squareup.picasso.Picasso;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.FWContentImgAdapter;
import com.sxm.yiwei.adapters.SearchGridAdapter;
import com.sxm.yiwei.adapters.SearchListviewAdapter;
import com.sxm.yiwei.beans.ShopContent;
import com.sxm.yiwei.tools.CircleTransform;
import com.sxm.yiwei.tools.ImgSwitcherActivity;
import com.sxm.yiwei.tools.IsLogin;
import com.sxm.yiwei.utils.InternetAddressUtis;
import com.sxm.yiwei.views.MyGridView;
import com.sxm.yiwei.views.MyListview;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class FWShopActivity extends BaseActivity implements View.OnClickListener{

    private MyListview shoplistview;
    private ScrollView fwScroll;
    private LinearLayout careme;
    private LinearLayout hiteme;
    private ImageView shopback;
    private LinearLayout concernshop;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private String shopid;
    private String longitude;
    private String latitude;
    private TextView shopname;
    private ImageView showimg1,showimg2,showimg3;
    private ImageView headimg;
    private TextView shopdistance;
    private TextView shopExplain;
    private ImageView shopbao;
    private ImageView shoptype;
    private ImageView shopxin;
    private ImageView shopzhifu;
    private TextView shopxinnumber;
    private TextView turnovernumber;
    private TextView serviceNumber;
    private ImageView care;
    private String id;
    private JSONArray showImages;
    private String isConcern;
    private AlertDialog dialog10;
    private MyGridView shopimgs;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fwshop);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        longitude = sharedPreferences.getString("longitude", "");
        latitude = sharedPreferences.getString("latitude", "");
        Intent intent = getIntent();
        shopid = intent.getStringExtra("id");
        fwScroll = (ScrollView) findViewById(R.id.fw_shop_scroll);
        fwScroll.smoothScrollTo(0,0);
        initShopView();
        getShopData();
    }
        // 获取店铺信息
    private void getShopData() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETSHOPDATA);
        params.addHeader("Content-Type","application/json");
        if (appId.equals("")){
            params.addHeader("appId","337cb33f-83bd-11e7-9812-00163e0a3457");
        }else {
            params.addHeader("appId",appId);
        }

        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("shopId",shopid);
        object.put("longitude",longitude);
        object.put("latitude",latitude);
        params.addParameter("params",object.toJSONString());

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                JSONObject obj1 = JSON.parseObject(data);
                String avatar = obj1.getString("avatar");
                String distance = obj1.getString("distance");
                String gradeCode = obj1.getString("gradeCode");
                id = obj1.getString("id");
                String intro = obj1.getString("intro");
                String isInsurance = obj1.getString("isInsurance");
                String name = obj1.getString("name");
                String sellGrade = obj1.getString("sellGrade");

                showImages = obj1.getJSONArray("showImages");
                JSONArray serviceList = obj1.getJSONArray("serviceList");
                String type = obj1.getString("type");
                String volume = obj1.getString("volume");
                isConcern = obj1.getString("isConcern");
                if (isConcern!=null){
                    if (isConcern.equals("true")){
                        care.setImageResource(R.mipmap.guanzhulanse);
                    }else {
                        care.setImageResource(R.mipmap.guanzhuhuise);

                    }
                }else {}

                shopname.setText(name);
                shopdistance.setText(distance+" 公里");
                shopExplain.setText("简介:"+intro);
                Picasso.with(FWShopActivity.this).load(InternetAddressUtis.IMG_URL+avatar).transform(new CircleTransform()).into(headimg);
                if (serviceList.size()>99){
                    serviceNumber.setText("99+");
                }else {
                    serviceNumber.setText(serviceList.size()+"");
                }
                turnovernumber.setText(volume+" 单");
                Integer geadfe = Integer.valueOf(gradeCode);
                switch (geadfe){
                    case 0:
                        shopzhifu.setImageResource(R.mipmap.shahuise);
                        break;
                    case 1:
                        shopzhifu.setImageResource(R.mipmap.g10);
                        break;
                    case 2:
                        shopzhifu.setImageResource(R.mipmap.g1);
                        break;
                    case 3:
                        shopzhifu.setImageResource(R.mipmap.g2);
                        break;
                    case 4:
                        shopzhifu.setImageResource(R.mipmap.g3);
                        break;
                    case 5:
                        shopzhifu.setImageResource(R.mipmap.g4);
                        break;
                    case 6:
                        shopzhifu.setImageResource(R.mipmap.g6);
                        break;
                    case 7:
                        shopzhifu.setImageResource(R.mipmap.g5);
                        break;
                    case 8:
                        shopzhifu.setImageResource(R.mipmap.g7);
                        break;
                    case 9:
                        shopzhifu.setImageResource(R.mipmap.g8);
                        break;
                    case 10:
                        shopzhifu.setImageResource(R.mipmap.g9);
                        break;

                }

                if (type.equals("2")){
                    shoptype.setVisibility(View.VISIBLE);
                }else {
                    shoptype.setVisibility(View.GONE);
                }
                Integer sell = Integer.valueOf(sellGrade);
                switch (sell){
                    case 0:
                        shopxin.setVisibility(View.GONE);
                        break;
                    case 1:
                        shopxin.setImageResource(R.mipmap.xin);
                        break;
                    case 2:
                        shopxinnumber.setText("2");
                        shopxin.setImageResource(R.mipmap.xin);
                        break;
                    case 3:
                        shopxinnumber.setText("3");
                        shopxin.setImageResource(R.mipmap.xin);
                        break;
                    case 4:
                        shopxinnumber.setText("4");
                        shopxin.setImageResource(R.mipmap.xin);
                        break;
                    case 5:
                        shopxinnumber.setText("5");
                        shopxin.setImageResource(R.mipmap.xin);
                        break;
                    case 6:
                        shopxin.setImageResource(R.mipmap.zuan);
                        break;
                    case 7:
                        shopxinnumber.setText("2");
                        shopxin.setImageResource(R.mipmap.zuan);
                        break;
                    case 8:
                        shopxinnumber.setText("3");
                        shopxin.setImageResource(R.mipmap.zuan);
                        break;
                    case 9:
                        shopxinnumber.setText("4");
                        shopxin.setImageResource(R.mipmap.zuan);
                        break;
                    case 10:
                        shopxinnumber.setText("5");
                        shopxin.setImageResource(R.mipmap.zuan);
                        break;
                    case 11:

                        shopxin.setImageResource(R.mipmap.gaun);
                        break;
                    case 12:
                        shopxinnumber.setText("2");
                       shopxin.setImageResource(R.mipmap.gaun);
                        break;
                    case 13:
                        shopxinnumber.setText("3");
                        shopxin.setImageResource(R.mipmap.gaun);
                        break;
                    case 14:
                        shopxinnumber.setText("4");
                        shopxin.setImageResource(R.mipmap.gaun);
                        break;
                    case 15:
                        shopxinnumber.setText("5");
                        shopxin.setImageResource(R.mipmap.gaun);
                        break;
                    case 16:
                        shopxin.setImageResource(R.mipmap.jinguan);
                        break;
                    case 17:
                        shopxinnumber.setText("2");
                        shopxin.setImageResource(R.mipmap.jinguan);
                        break;
                    case 18:
                        shopxinnumber.setText("3");
                        shopxin.setImageResource(R.mipmap.jinguan);
                        break;
                    case 19:
                        shopxinnumber.setText("4");
                        shopxin.setImageResource(R.mipmap.jinguan);
                        break;
                    case 20:
                        shopxinnumber.setText("5");
                        shopxin.setImageResource(R.mipmap.jinguan);
                        break;
                }

                if (isInsurance.equals("false")){
                    shopbao.setVisibility(View.GONE);
                }else {
                    shopbao.setVisibility(View.VISIBLE);
                }
                if (type.equals("1")){
                    shoptype.setVisibility(View.GONE);
                }else {
                    shoptype.setVisibility(View.VISIBLE);
                }

                FWContentImgAdapter imgAdapter = new FWContentImgAdapter(FWShopActivity.this,showImages);
                shopimgs.setAdapter(imgAdapter);

                int itemWidth = (int)(125 * shopimgs.getResources().getDisplayMetrics().density);
                int itemSize = showImages.size();
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemSize * itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
                shopimgs.setLayoutParams(params);
                shopimgs.setNumColumns(itemSize);
                shopimgs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent in = new Intent(FWShopActivity.this, ImgSwitcherActivity.class);
                        in.putExtra("position",position);
                        in.putExtra("imgs",showImages);
                        startActivity(in);
                    }
                });


                List<ShopContent.DataBean.ServiceListBean> shopList = new ArrayList<>();
                for (int i = 0; i < serviceList.size(); i++) {
                    JSONObject obj2 = JSON.parseObject(String.valueOf(serviceList.get(i)));
                    String amount = obj2.getString("amount");
                    String id1 = obj2.getString("id");
                    String intro1 = obj2.getString("intro");
                    String name1 = obj2.getString("name");
                    String score = obj2.getString("score");
                    String serviceAvatar = obj2.getString("serviceAvatar");
                    String volume1 = obj2.getString("volume");
                    ShopContent.DataBean.ServiceListBean serviceListBean = new ShopContent.DataBean.ServiceListBean();
                    serviceListBean.setAmount(Double.valueOf(amount));
                    serviceListBean.setId(id1);
                    serviceListBean.setIntro(intro1);
                    serviceListBean.setName(name1);
                    if (score!=null){
                        serviceListBean.setScore(Double.valueOf(score));
                    }else {
                        serviceListBean.setScore(0.0);
                    }

                    serviceListBean.setServiceAvatar(serviceAvatar);
                    serviceListBean.setVolume(Integer.valueOf(volume1));
                    shopList.add(serviceListBean);
                }
                final SearchListviewAdapter adapter = new SearchListviewAdapter(FWShopActivity.this,shopList,0);
                shoplistview.setAdapter(adapter);
                shoplistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent i = new Intent(FWShopActivity.this,FWContentActivity.class);
                        ShopContent.DataBean.ServiceListBean item = (ShopContent.DataBean.ServiceListBean) adapter.getItem(position);
                        String serviceid = item.getId();
                        i.putExtra("id",serviceid);
                        startActivity(i);
                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initShopView() {
        shopimgs = (MyGridView) findViewById(R.id.fw_shop_gridimgs);
        care = (ImageView) findViewById(R.id.fw_shop_care);
        shopback = (ImageView) findViewById(R.id.fw_shop_back);
        shoplistview = (MyListview) findViewById(R.id.fw_shop_listview);
        careme = (LinearLayout) findViewById(R.id.fw_shop_careme);
        hiteme = (LinearLayout) findViewById(R.id.fw_shop_hiteme);
        concernshop = (LinearLayout) findViewById(R.id.fw_shop_concern);
        backmain = (ImageView) findViewById(R.id.fw_shop_backmain);
        shopname = (TextView) findViewById(R.id.fw_shop_shopname);
        headimg = (ImageView) findViewById(R.id.fw_shop_headimg);
        shopdistance = (TextView) findViewById(R.id.fw_shop_distance);
        shopExplain = (TextView) findViewById(R.id.fw_shop_explain);
        shopbao = (ImageView) findViewById(R.id.fw_shop_shopbao);
        shoptype = (ImageView) findViewById(R.id.fw_shop_shoptype);
        shopxin = (ImageView) findViewById(R.id.fw_shop_shopxin);
        shopzhifu = (ImageView) findViewById(R.id.fw_shop_shopzhifu);
        shopxinnumber = (TextView) findViewById(R.id.fw_shop_shopxinnumber);
        turnovernumber = (TextView) findViewById(R.id.fw_shop_turnovernumber);
        serviceNumber = (TextView) findViewById(R.id.fw_shop_servicenumber);


        careme.setOnClickListener(this);
        hiteme.setOnClickListener(this);
        shopback.setOnClickListener(this);
        concernshop.setOnClickListener(this);
        backmain.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fw_shop_backmain:
                startActivity(new Intent(FWShopActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.fw_shop_back:
                finish();
                break;
            case R.id.fw_shop_careme:
                Toast.makeText(this, "功能开发中，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.fw_shop_concern:
               if (!token.equals("")){
                   AlertDialog.Builder builder10 = new AlertDialog.Builder(FWShopActivity.this);
                   View view10 = LayoutInflater.from(FWShopActivity.this).inflate(R.layout.cancel_servicedialog, null);
                   builder10.setView(view10);
                   Button cancelbt = (Button) view10.findViewById(R.id.cancel_cancelbt);
                   Button surebt = (Button) view10.findViewById(R.id.cancel_surebt);
                   TextView t2 = (TextView) view10.findViewById(R.id.cancel_t2);
                   cancelbt.setText("否");
                   surebt.setText("是");
                   if (isConcern.equals("true")){
                       t2.setText("是否取消关注");
                   }else {
                       t2.setText("是否关注");
                   }

                   dialog10 = builder10.create();
                   cancelbt.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           dialog10.dismiss();
                       }
                   });
                   surebt.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           concernShop();
                           dialog10.dismiss();
                       }
                   });
                   dialog10.show();
               }else {
                   IsLogin.isLogin();
               }

                break;
            case R.id.fw_shop_hiteme:
               if (!token.equals("")){
                   Intent in = new Intent(FWShopActivity.this,FWHiteMeShopActivity.class);
                   in.putExtra("from","shop");
                   in.putExtra("id",id);
                   startActivity(in);
               }else {
                   IsLogin.isLogin();
               }
                break;

        }
    }
        // 关注店铺
    public void concernShop() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.SHOPCONCERN);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);

        JSONObject object = new JSONObject();
        object.put("shopId",shopid);

        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("tag", "onSuccess: 关注---"+result);
                if (isConcern.equals("true")){
                    isConcern="false";
                    care.setImageResource(R.mipmap.guanzhu11111tab);
                    Toast.makeText(FWShopActivity.this, "已取消关注", Toast.LENGTH_SHORT).show();

                }else {
                    isConcern="true";
                    care.setImageResource(R.mipmap.weiguanzhutab);
                    Toast.makeText(FWShopActivity.this, "关注店铺成功", Toast.LENGTH_SHORT).show();

                }
                concernshop.setClickable(true);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 关注---"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

}
